package com.moco.launcher.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

import com.moco.launcher.LauncherApplication;
import com.moco.launcher.R;
import com.moco.launcher.fragment.VoiceFragment;
import com.moco.launcher.listener.FragmentListener;
import com.moco.launcher.listener.VoicePlayListener;
import com.moco.launcher.play.MocoPlayerClient;
import com.oazon.moco.bean.Metadata;
import com.oazon.moco.lan.core.http.bean.TransportState;
import com.oazon.moco.link.SaveMediaState;
import com.oazon.common.Logger;
import com.oazon.common.Assert;

public class VoiceActivity extends BaseFragmentActivity implements
		FragmentListener {
	private static final String TAG = "VoiceActivity";

	private LauncherApplication mApp = null;
	private VoiceFragment fragment1;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.pop_layout);

		Intent intent = this.getIntent();
		String type = intent.getStringExtra("fragment_type");
		Assert.notNEWS(type);
		Logger.d(TAG, "type=" + type);

		if (type.equals("voice")) {
			boolean isExit = intent.getBooleanExtra("isExit", false);
			if (isExit) {
				finish();
				return;
			}

			MocoPlayerClient.getInstance().iPause(false,true);
			fragment1 = new VoiceFragment();
			fragment1.registerListener(new VoicePlayListener() {
				@Override
				public void onMusicListSelected(ArrayList<Metadata> items) {
					if (items == null) {
						if (SaveMediaState.getInstance().getTransportState() != TransportState.PAUSED_PLAYBACK) {
							Intent intent = new Intent(VoiceActivity.this,
									MainActivity.class);
							intent.putExtra("action", "play");
							//intent.putExtra("fragment_type", "VoiceFragment");
							VoiceActivity.this.startActivity(intent);
						}
					} else {
						Intent intent = new Intent(VoiceActivity.this,
								MainActivity.class);
						intent.putParcelableArrayListExtra("voice_result",
								items);
						intent.putExtra("fragment_type", "VoiceFragment");
						VoiceActivity.this.startActivity(intent);
					}
					finishActivity();
				}
				
			});

			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment, fragment1).commit();
		}
		Assert.notNull(fragment1);
		fragment1.registerCallback(this);

		mApp = ((LauncherApplication) getApplication());
		mApp.registerActivty(this);
		mApp.setShowVoiceFragment(true);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		setIntent(intent);

		String type = intent.getStringExtra("fragment_type");
		Logger.d(TAG, "type=" + type);
		Assert.notNEWS(type);
		if (type.equals("voice")) {
			boolean isExit = intent.getBooleanExtra("isExit", false);
			Logger.i(TAG, "isExtit==" + isExit);
			if (isExit) {
				finishActivity();
				return;
			}
		}
	}

	@Override
	public void finishActivity() {
		this.finish();
		overridePendingTransition(R.anim.pop_in, R.anim.pop_out);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		Logger.d(TAG, "the launcher receive dispatchTouchEvent event");
		mApp.resetStartTime();
		return super.dispatchTouchEvent(event);
	}

	@Override
	protected void onDestroy() {
		mApp.setShowVoiceFragment(false);
		Logger.i(TAG, "setShowVoiceFragment(false)");
		super.onDestroy();
	}
}
